package org.dragonnova.meetingapp.app.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import org.dragonnova.meetingapp.R;
import org.dragonnova.meetingapp.app.fragment.guide.FragmentImpl;
import org.dragonnova.meetingapp.app.fragment.guide.MeetingCreateFragment;
import org.dragonnova.meetingapp.app.fragment.guide.RouterFragment;
import org.dragonnova.meetingapp.app.fragment.guide.WifiConnectFragment;
import org.dragonnova.meetingapp.core.manager.UdpManager;
import org.dragonnova.meetingapp.model.bean.MeetingInfo;
import org.dragonnova.meetingapp.app.service.WIFIServiceTest;
import org.dragonnova.meetingapp.utils.Constants;
import org.dragonnova.meetingapp.utils.ContentResolverUtil;
import org.dragonnova.meetingapp.utils.LogUtil;
import org.dragonnova.meetingapp.utils.ToastUtil;
import org.dragonnova.meetingapp.utils.UIHelper;

import java.util.ArrayList;
import java.util.List;


public class MeetingGuideActivity extends AppCompatActivity implements FragmentImpl.OnFragmentInteractionListener {

    private FragmentManager mFragmentManager;
    private ArrayList<Fragment> mFragmentList = new ArrayList<>();

    private WifiManager mWifiManager;
    private WifiReceiver mWifiReceiver;
    private StateImp mState;

    private WIFIServiceTest mWifiService;//wifi服务
    private WIFIServiceTest.WifiBinder mWifiBinder;

    private FrameLayout mguide_fl_container;
    private ProgressBar mguide_pb_progress;

    private boolean mIsProgressShow;
    private ServiceConnection mServiceConnection;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_meeting_guide);
        LogUtil.eee("activity created==============");

        Intent intent = getIntent();
        if (intent != null) {
            int action = intent.getIntExtra(Constants.ACTIVITY_INTENT_ACTION, -1);
            if (action >= 0) {
                switch (action) {
                    case Constants.ACTIVITY_ACTION_CREATOR:
                        mState = new CreatorState();
                        break;
                    case Constants.ACTIVITY_ACTION_PARTICIPANT:
                        mState = new ParticipantState();
                        break;
                    case Constants.ACTIVITY_ACTION_RESETOR:
                        mState = new ResetorState();
                        break;
                    default:
                        throw new RuntimeException("MeetingGuideActivity received an action that not defined");
                }
            } else {
                throw new RuntimeException("MeetingGuideActivity received an action that not defined");
            }
        }

        initData();
        initView();

    }

    private void initView() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) getSupportActionBar().setDisplayShowTitleEnabled(false);

        mguide_fl_container = (FrameLayout) findViewById(R.id.mguide_fl_container);
        mguide_pb_progress = (ProgressBar) findViewById(R.id.mguide_pb_progress);

        mFragmentManager = getSupportFragmentManager();

        mState.started();
    }

    private void initData() {
        mWifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

        /* 绑定service监听*/
        mServiceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                if (service instanceof WIFIServiceTest.WifiBinder) {
                    mWifiBinder = (WIFIServiceTest.WifiBinder) service;
                    mWifiService = ((WIFIServiceTest.WifiBinder) service).getService();
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LogUtil.eee("activity destoryed==============");
        unRegisterBroadcastReceiver();
        unbindService();

    }

    @Override
    protected void onStart() {
        super.onStart();
        bindService();
        registerBroadcastReceiver();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //unbindService();
        //unRegisterBroadcastReceiver();
    }

    /**
     * 绑定服务
     */
    private void bindService() {
        //启动服务
        Intent wifiIntent = new Intent(this, WIFIServiceTest.class);
        bindService(wifiIntent, mServiceConnection, BIND_AUTO_CREATE);
    }

    /**
     * 解除服务绑定
     */
    private void unbindService() {
        unbindService(mServiceConnection);
        LogUtil.eee("unbind service successfully");
    }

    /**
     * 注册广播
     */
    private void registerBroadcastReceiver() {
        mWifiReceiver = new WifiReceiver();
        IntentFilter filter2 = new IntentFilter();
        filter2.addAction(Constants.WIFI_BORADCAST_ACTION);
        registerReceiver(mWifiReceiver, filter2);
    }

    private void unRegisterBroadcastReceiver() {
        unregisterReceiver(mWifiReceiver);
    }

    @Override
    public void onFragmentInteraction(Uri uri) {
        String fragment = uri.getHost();
        List<String> pathSegments = uri.getPathSegments();
        String action = pathSegments.get(0);
        LogUtil.eee("fragment:" + fragment + "====================" + "pathSegments:" + pathSegments.toString());
        switch (fragment) {
            case "WifiConnectFragment":
                switch (action) {
                    case "wifi":
                        mState.connect(pathSegments);
                        break;
                    case "skip":
                        mState.connected();
                        break;
                }
                break;
            case "RouterFragment":
                switch (action) {
                    case "skip":
                        mState.group();
                        break;
                }
                break;
            case "MeetingCreateFragment":
                switch (action) {
                    case "create":
                        mState.create(pathSegments);
                        break;
                    case "cancel":
                        this.finish();
                        break;
                }
                break;
        }
    }

    private class CreatorState implements StateImp {

        Status currentStatus;

        @Override
        public void started() {
            if (currentStatus != Status.STARTED) {
                currentStatus = Status.STARTED;
                hideFragments();
                Fragment Frag = mFragmentManager.findFragmentByTag("WifiConnectFragment");
                if (Frag != null) {
                    mFragmentManager.beginTransaction().show(Frag).commit();
                } else {
                    WifiConnectFragment wifiConnectFragment = WifiConnectFragment.newInstance("null", "null");
                    mFragmentManager.beginTransaction().add(R.id.mguide_fl_container, wifiConnectFragment, "WifiConnectFragment").addToBackStack(null).commit();
                    mFragmentList.add(wifiConnectFragment);
                }
            }
        }

        @Override
        public void connect(List<String> pathSegments) {
            showProgress(true);
            if (mWifiBinder != null) {
                mWifiBinder.setWifiInfo(pathSegments.get(1), pathSegments.get(2));
            }
        }

        @Override
        public void connected() {
            showProgress(false);
            if (currentStatus != Status.CONNECTED) {
                currentStatus = Status.CONNECTED;
                hideFragments();
                Fragment Frag = mFragmentManager.findFragmentByTag("RouterFragment");
                if (Frag != null) {
                    mFragmentManager.beginTransaction().show(Frag).commitAllowingStateLoss();
                } else {
                    RouterFragment routerFragment = RouterFragment.newInstance("http://tplogin.cn/", "null");
                    mFragmentManager.beginTransaction().add(R.id.mguide_fl_container, routerFragment, "RouterFragment").addToBackStack(null).commit();
                    mFragmentList.add(routerFragment);
                }
            }
        }

        @Override
        public void ping() {

        }

        @Override
        public void pingSuccess() {
            currentStatus = Status.PINGSUCCESS;
        }

        @Override
        public void group() {
            showProgress(true);
            if (mWifiBinder != null) {
                mWifiBinder.startGroup();
            }
        }

        @Override
        public void grouped() {
            showProgress(false);
            if (currentStatus != Status.GROUPED) {
                currentStatus = Status.GROUPED;
                hideFragments();
                Fragment Frag = mFragmentManager.findFragmentByTag("MeetingCreateFragment");
                if (Frag != null) {
                    mFragmentManager.beginTransaction().show(Frag).commit();
                } else {
                    int meetingId = MeetingInfo.getRandomId();
                    MeetingCreateFragment meetingCreateFragment = MeetingCreateFragment.newInstance(Constants.ACTIVITY_ACTION_CREATOR, Long.toString(meetingId));
                    mFragmentManager.beginTransaction().add(R.id.mguide_fl_container, meetingCreateFragment, "MeetingCreateFragment").addToBackStack(null).commit();
                    mFragmentList.add(meetingCreateFragment);
                }
            }
        }

        @Override
        public void create(List<String> pathSegments) {
            showProgress(true);
            if (pathSegments.size() == 3) {
                if (mWifiBinder != null) {
                    mWifiBinder.createMeeting(new MeetingInfo(Long.parseLong(pathSegments.get(1)), pathSegments.get(2), ""));
                }
            } else if (pathSegments.size() == 4) {
                if (mWifiBinder != null) {
                    mWifiBinder.createMeeting(new MeetingInfo(Long.parseLong(pathSegments.get(1)), pathSegments.get(2), pathSegments.get(3)));
                }
            }
        }

        @Override
        public void created(MeetingInfo meetingInfo) {
            showProgress(false);
            if (currentStatus != Status.CREATED) {
                currentStatus = Status.CREATED;
                hideFragments();
                Intent intent = new Intent(MeetingGuideActivity.this, RecordActivity.class);
                intent.putExtra(Constants.ACTIVITY_INTENT_ACTION, Constants.ACTIVITY_ACTION_CREATOR);
                intent.putExtra(Constants.ACTIVITY_INTENT_ACTION_EXTRA, meetingInfo);

                MeetingGuideActivity.this.startActivity(intent);
                MeetingGuideActivity.this.finish();
            }
        }

        @Override
        public void participate() {

        }

        @Override
        public void participated(MeetingInfo meetingInfo) {

        }

        @Override
        public void unconnected() {
            currentStatus = Status.UNCONNECTED;
        }

        @Override
        public void ungrouped() {

        }

        @Override
        public void error() {
            currentStatus = Status.ERROR;
        }

        @Override
        public void onBackPressed() {
            if (mIsProgressShow) {
                LogUtil.eee("on back pressed : hide the progress");
                showProgress(false);
            } else {
                LogUtil.eee("on back pressed : currentStatus = " + currentStatus);
            }
            switch (currentStatus) {
                case STARTED:
                    MeetingGuideActivity.this.finish();
                    break;
                case CONNECTED:
                    hideFragments();
                    started();
                    break;
                case GROUPED:
                    hideFragments();
                    if (mWifiBinder != null) {
                        mWifiBinder.stopGroup();
                    }
                    connected();
                    break;
                default:
                    finish();
                    break;
            }
        }
    }

    private class ParticipantState implements StateImp {

        Status currentStatus;

        @Override
        public void started() {
            if (currentStatus != Status.STARTED) {
                currentStatus = Status.STARTED;
                hideFragments();
                Fragment Frag = mFragmentManager.findFragmentByTag("WifiConnectFragment");
                if (Frag != null) {
                    mFragmentManager.beginTransaction().show(Frag).commit();
                } else {
                    WifiConnectFragment wifiConnectFragment = WifiConnectFragment.newInstance("null", "null");
                    mFragmentManager.beginTransaction().add(R.id.mguide_fl_container, wifiConnectFragment, "WifiConnectFragment").addToBackStack(null).commit();
                    mFragmentList.add(wifiConnectFragment);
                }
            }
        }

        @Override
        public void connect(List<String> pathSegments) {
            showProgress(true);
            if (mWifiBinder != null) {
                mWifiBinder.setWifiInfo(pathSegments.get(1), pathSegments.get(2));
            }
        }

        @Override
        public void connected() {
            showProgress(false);
            if (currentStatus != Status.CONNECTED) {
                currentStatus = Status.CONNECTED;
                group();
            }
        }

        @Override
        public void ping() {

        }

        @Override
        public void pingSuccess() {
            currentStatus = Status.PINGSUCCESS;
        }

        @Override
        public void group() {
            showProgress(true);
            if (mWifiBinder != null) {
                mWifiBinder.startGroup();
            }
        }

        @Override
        public void grouped() {
            //showProgress(false);
            if (currentStatus != Status.GROUPED) {
                currentStatus = Status.GROUPED;
                SystemClock.sleep(200);
                participate();
            }
        }

        @Override
        public void create(List<String> pathSegments) {

        }

        @Override
        public void created(MeetingInfo meetingInfo) {
            showProgress(false);
            currentStatus = Status.CREATED;
        }

        @Override
        public void participate() {
            if (mWifiBinder != null) {
                mWifiBinder.joinMeeting();
            }
        }

        @Override
        public void participated(MeetingInfo meetingInfo) {
            showProgress(false);
            if (currentStatus != Status.PARTICIPATED) {
                currentStatus = Status.PARTICIPATED;
                Intent intent = new Intent(MeetingGuideActivity.this, RecordActivity.class);
                intent.putExtra(Constants.ACTIVITY_INTENT_ACTION, Constants.ACTIVITY_ACTION_PARTICIPANT);
                intent.putExtra(Constants.ACTIVITY_INTENT_ACTION_EXTRA, meetingInfo);
                MeetingGuideActivity.this.startActivity(intent);
                MeetingGuideActivity.this.finish();
            }
        }

        @Override
        public void unconnected() {
            currentStatus = Status.UNCONNECTED;
        }

        @Override
        public void ungrouped() {

        }

        @Override
        public void error() {
            currentStatus = Status.ERROR;
        }

        @Override
        public void onBackPressed() {
            if (mIsProgressShow) {
                LogUtil.eee("on back pressed : hide the progress");
                showProgress(false);
            } else {
                LogUtil.eee("on back pressed : currentStatus = " + currentStatus);
            }
            switch (currentStatus) {
                case STARTED:
                    MeetingGuideActivity.this.finish();
                    break;
                case CONNECTED:
                    hideFragments();
                    if (mWifiBinder != null) {
                        mWifiBinder.stopGroup();
                    }
                    started();
                    break;
                default:
                    finish();
                    break;
            }
        }
    }

    private class ResetorState implements StateImp {

        Status currentStatus;

        @Override
        public void started() {
            grouped();
        }

        @Override
        public void connect(List<String> pathSegments) {

        }

        @Override
        public void connected() {

        }

        @Override
        public void ping() {

        }

        @Override
        public void pingSuccess() {

        }

        @Override
        public void group() {

        }

        @Override
        public void grouped() {
            if (currentStatus != Status.GROUPED) {
                currentStatus = Status.GROUPED;
                hideFragments();
                Fragment Frag = mFragmentManager.findFragmentByTag("MeetingCreateFragment");
                if (Frag != null) {
                    mFragmentManager.beginTransaction().show(Frag).commit();
                } else {
                    MeetingCreateFragment meetingCreateFragment = MeetingCreateFragment.newInstance(Constants.ACTIVITY_ACTION_RESETOR, "null");
                    mFragmentManager.beginTransaction().add(R.id.mguide_fl_container, meetingCreateFragment, "MeetingCreateFragment").addToBackStack(null).commit();
                    mFragmentList.add(meetingCreateFragment);
                }
            }
        }

        @Override
        public void create(List<String> pathSegments) {
            showProgress(true);
            if (pathSegments.size() == 3) {
                if (mWifiBinder != null) {
                    mWifiBinder.resetMeeting(new MeetingInfo(Long.parseLong(pathSegments.get(1)), pathSegments.get(2), ""));
                }
            } else if (pathSegments.size() == 4) {
                if (mWifiBinder != null) {
                    mWifiBinder.resetMeeting(new MeetingInfo(mWifiBinder.getMeetingInfo().getSerial(), pathSegments.get(2), pathSegments.get(3)));
                }
            }
        }

        @Override
        public void created(MeetingInfo meetingInfo) {
            if (currentStatus != Status.CREATED) {
                currentStatus = Status.CREATED;
                hideFragments();
                Intent intent = new Intent(MeetingGuideActivity.this, RecordActivity.class);
                intent.putExtra(Constants.ACTIVITY_INTENT_ACTION, Constants.ACTIVITY_ACTION_CREATOR);
                intent.putExtra(Constants.ACTIVITY_INTENT_ACTION_EXTRA, meetingInfo);
                MeetingGuideActivity.this.startActivity(intent);
                MeetingGuideActivity.this.finish();
            }
        }

        @Override
        public void participate() {

        }

        @Override
        public void participated(MeetingInfo meetingInfo) {

        }

        @Override
        public void unconnected() {

        }

        @Override
        public void ungrouped() {

        }

        @Override
        public void error() {

        }

        @Override
        public void onBackPressed() {
            finish();
        }
    }

    @Override
    public void onBackPressed() {
        mState.onBackPressed();
    }

    public void hideFragment(Fragment fragment) {
        if (!fragment.isHidden()) {
            mFragmentManager.beginTransaction().hide(fragment).commit();
        }
    }

    public void showFragment(Fragment fragment) {
        if (fragment.isHidden()) {
            mFragmentManager.beginTransaction().show(fragment).commit();
        }
    }

    public void hideFragments() {
        for (Fragment fragment : mFragmentList) {
            if (!fragment.isHidden()) {
                mFragmentManager.beginTransaction().hide(fragment).commit();
            }
        }
    }

    /**
     * service发送的广播
     */
    private class WifiReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Constants.WIFI_BORADCAST_ACTION)) {
                String option = intent.getStringExtra(Constants.BORADCAST_INTENT_OPTION);
                switch (option) {
                    case WIFIServiceTest.KEY_HANDLER_WIFI_CONNECT:
                        mState.connected();
                        break;
                    case UdpManager.KEY_GROUP_CONNECTED_STATUS:
                        mState.grouped();
                        break;
                    case UdpManager.KEY_HANDLER_MEETING_CREATED:
                        MeetingInfo meetingInfo1 = (MeetingInfo) (intent.getSerializableExtra(UdpManager.KEY_HANDLER_MEETING_CREATED));
                        int id1 = saveMeeting(meetingInfo1);
                        if (mWifiBinder != null) {
                            mWifiBinder.setMeetingId(id1);
                        }
                        mState.created(meetingInfo1);
                        break;
                    case UdpManager.KEY_HANDLER_MEETING_ALREADY_CREATED:
                        showProgress(false);
                        ToastUtil.showTip("会议已经创建，无法重复创建");
                        showRecoDialog();
                        break;
                    case UdpManager.KEY_HANDLER_MEETING_JOINED:
                        ToastUtil.showTip("会议正在进行，欢迎加入");
                        MeetingInfo meetingInfo2 = (MeetingInfo) (intent.getSerializableExtra(UdpManager.KEY_HANDLER_MEETING_JOINED));
                        int id2 = saveMeeting(meetingInfo2);
                        if (mWifiBinder != null) {
                            mWifiBinder.setMeetingId(id2);
                        }
                        mState.participated(meetingInfo2);
                        break;
                    case UdpManager.KEY_HANDLER_MEETING_OVER:
                        showProgress(false);
                        ToastUtil.showTip("会议已经结束，暂时无法加入");
                        MeetingGuideActivity.this.finish();
                        break;
                    case UdpManager.KEY_HANDLER_MEETING_CLOSED:
                        ToastUtil.showTip("您已经关闭会议");
                        break;
                    case UdpManager.KEY_HANDLER_MEETING_HAS_OVER:
                        ToastUtil.showTip("会议已经关闭，不用重复操作");
                        break;
                    case UdpManager.KEY_HANDLER_MEETING_DISCONNECTED:
                        new AlertDialog.Builder(MeetingGuideActivity.this).setTitle("会议已经失去连接").setPositiveButton("离开", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                mWifiBinder.stopGroup();
                                mState.started();
                            }
                        }).setCancelable(false).show();
                        break;
                    case UdpManager.KEY_HANDLER_MEETING_RESETED:
                        ToastUtil.showTip("备注成功");
                        MeetingInfo meetingInfo3 = (MeetingInfo) (intent.getSerializableExtra(UdpManager.KEY_HANDLER_MEETING_RESETED));
                        resetMeeting(meetingInfo3);
                        mState.created(meetingInfo3);
                        break;
                    case UdpManager.KEY_HANDLER_MEETING_RESET_ERROR:
                        ToastUtil.showTip("备注修改未成功");
                        break;
                    default:
                        break;
                }
            }
        }
    }

    /**
     * 状态
     */
    private interface StateImp {

        enum Status {
            STARTED, CONNECTED, PINGSUCCESS, GROUPED, PARTICIPATED, CREATED, UNCONNECTED, UNGROUPED, ERROR
        }

        void started();

        void connect(List<String> pathSegments);

        void connected();

        void ping();

        void pingSuccess();

        void group();

        void grouped();

        void create(List<String> pathSegments);

        void created(MeetingInfo meetingInfo);

        void participate();

        void participated(MeetingInfo meetingInfo);

        void unconnected();

        void ungrouped();

        void error();

        void onBackPressed();
    }

    /**
     * Shows the progress UI and hides the login form.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
    private void showProgress(final boolean show) {
        // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
        // for very easy animations. If available, use these APIs to fade-in
        // the progress spinner.

        mIsProgressShow = show;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
            int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);

            mguide_fl_container.setVisibility(show ? View.GONE : View.VISIBLE);
            mguide_fl_container.animate().setDuration(shortAnimTime).alpha(
                    show ? 0 : 1).setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    mguide_fl_container.setVisibility(show ? View.GONE : View.VISIBLE);
                }
            });

            mguide_pb_progress.setVisibility(show ? View.VISIBLE : View.GONE);
            mguide_pb_progress.animate().setDuration(shortAnimTime).alpha(
                    show ? 1 : 0).setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    mguide_pb_progress.setVisibility(show ? View.VISIBLE : View.GONE);
                }
            });
        } else {
            // The ViewPropertyAnimator APIs are not available, so simply show
            // and hide the relevant UI components.
            mguide_pb_progress.setVisibility(show ? View.VISIBLE : View.GONE);
            mguide_pb_progress.setVisibility(show ? View.GONE : View.VISIBLE);
        }
    }

    private int saveMeeting(MeetingInfo meetingInfo) {
        ContentResolverUtil contentResolverUtil = new ContentResolverUtil(this);
        int id = contentResolverUtil.findMeetingBySerial(meetingInfo.getSerial());
        if (id <= 0) {
            return contentResolverUtil.insertMeeting(meetingInfo);
        } else {
            contentResolverUtil.updateMeeting(meetingInfo);
            return id;
        }
    }

    private int resetMeeting(MeetingInfo meetingInfo) {
        ContentResolverUtil contentResolverUtil = new ContentResolverUtil(this);
        return contentResolverUtil.updateMeeting(meetingInfo);
    }

    private Dialog recoDialog;

    /**
     * 语音输入的提示框
     */
    @SuppressLint("InflateParams")
    private void showRecoDialog() {
        if (recoDialog == null) {
            recoDialog = UIHelper.getCircularCornerDialog(this);
            Button recoNegButton = (Button) recoDialog.findViewById(R.id.dialog_btn_neg);
            Button recoPosButton = (Button) recoDialog.findViewById(R.id.dialog_btn_pos);
            recoNegButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    //语音识别取消
                    if (recoDialog.isShowing()) {
                        recoDialog.cancel();
                    }
                }
            });
            recoPosButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    //语音识别完成
                    if (recoDialog.isShowing()) {
                        recoDialog.cancel();
                    }
                    if (mWifiBinder != null) {
                        mWifiBinder.closeMeeting();
                    }
                }
            });

            recoDialog = UIHelper.setCircularCornerDialogContent(recoDialog, getString(R.string.action_meeting_close), getString(R.string.action_meeting_yes), getString(R.string.action_meeting_no));
            recoDialog.setCanceledOnTouchOutside(false);
            recoDialog.setCancelable(false);
        }

        recoDialog.show();
    }
}
